<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\components;

use app\models\User;
use Yii;

/**
 * Description of SessionUser
 *
 * @author shuai
 */
class SessionUser implements \yii\web\IdentityInterface {

    public $id;
    public $username;
    public $password_time;
    public $authKey;
    public $role_type;
    public $bindtype;
    public $avatar;
    public $time;
    public $regtime;
    public $open_id;
    static $_identity;

    public static function findIdentity($id) {

        if (self::$_identity === null) {
            /// static::$_identity=static::findOne($id);
            self::$_identity = static::session_findOne($id);
        }

        return self::$_identity;
    }

    /**
     * 如果没有cookie,返回null
     */
    public static function getCookieModel($uid) {
        $cookies = Yii::$app->request->cookies;
        return $cookies->getValue('user' . $uid);
    }

    /**
     * 添加一个cookie
     */
    public static function setCookeyModel($uid, $arrModel) {
        $cookies = Yii::$app->response->cookies;
        // 在要发送的响应中添加一个新的cookie
        $cookies->add(new \yii\web\Cookie([
            'name' => 'user' . $uid,
            'value' => $arrModel,
        ]));
    }

    /**
     * 获取存于内存中的用户对象
     */
    public static function session_findOne($id) {
        //  $sessionmodel=Yii::$app->session->get('user'.$id); 
        /// var_dump($id);exit;
        $sessionmodel = self::getCookieModel($id);
        $self = new self;
        $self->id = $id;
        //echo 'see:';  var_dump($sessionmodel);exit;
        $model = new self;
        if (!$sessionmodel) {
            $sessionmodel = [];
            $model = User::findOne($id);
            if (empty($model)) {
                Yii::$app->getResponse()->getCookies()->remove(new \yii\web\Cookie(Yii::$app->user->identityCookie));
                Yii::$app->getSession()->destroy();
                /// exit('dddddddddd');

                return NULL;
            }

            $sessionmodel['id'] = $model->id;
            $sessionmodel['username'] = $model->username;
            $sessionmodel['password_time'] = $model->password_time;
            $sessionmodel['avatar'] = $model->avatar;
            $sessionmodel['time'] = $model->time;
            $sessionmodel['open_id'] = $model->open_id;
            $sessionmodel['authKey'] = $model->authKey;
            // Yii::$app->session->set('user'.$id,$sessionmodel); 
            self::setCookeyModel($id, $sessionmodel);
        } 
        // $self->id=$sessionmodel['id'];
        $self->username = $sessionmodel['username'];
        $self->password_time = $sessionmodel['password_time'];
        $self->avatar = $sessionmodel['avatar'];
        $self->time = $sessionmodel['time'];
        $self->open_id = $sessionmodel['open_id'];
        $self->authKey = $sessionmodel['authKey'];
        return $self;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey) {
       // var_dump($this);exit;

        return $this->authKey == $authKey;
    }

    public static function findIdentityByAccessToken($token, $type = null) {
        return User::findOne(['access_token' => $token]);
    }

    /**
     * 判断是否为管理员权限
     */
    public function isAdmin() {


        $model = User::findOne($this->id);
        /*  $c=  $this->getUptime();
          if(!$c){
          return false;
          }
          //var_dump($c,$model->password_time);exit;
          if($c!==$model->password_time){
          return false;
          } */
        if ($model->role_type === User::role_type_admin) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取保存在cookie的密码更新时间
     */
    private function getUptime() {
        $cookies = Yii::$app->request->cookies;
        $val = $cookies->getValue('wuptime');
        if ($val === null) {
            return false;
        }
        return $val;
    }

    /**
     * @inheritdoc
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey() {

        return $this->authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password) {

        $model = User::findOne($this->id);
        return $model->password === User::toPassword($password);
    }

    /**
     * 修改密码
     */
    public function upPassword($password) {
        $model = User::findOne($this->id);
        $model->password = User::toPassword($password);
        $model->password_time = time();
        $model->update();
    }

}
